package com.peanut.baby.mvp.mydata;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.peanut.baby.R;
import com.peanut.baby.mvp.BaseMvpActivity;
import com.peanut.baby.mvp.mydata.daily.DailyDataFragment;
import com.peanut.devlibrary.adapter.FragmentTabAdapter;
import com.peanut.devlibrary.widget.TitleLayout;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by anonymouslystudio on 2017/8/28.
 */

public class MyDataActivity extends BaseMvpActivity  implements RadioGroup.OnCheckedChangeListener,
        ViewPager.OnPageChangeListener,
        TitleLayout.OnTitleClickListener{

    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.data_radio_group)
    RadioGroup radioGroup;
    @BindView(R.id.data_viewpager)
    ViewPager viewpager;

    ArrayList<Fragment> fragments;
    FragmentTabAdapter adapter;

    public static void start(Context ctx) {
        ctx.startActivity(new Intent(ctx, MyDataActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mydata);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        title.setOnTitleClickedListener(this);


        viewpager.setOnPageChangeListener(this);
        radioGroup.setOnCheckedChangeListener(this);

        fragments = new ArrayList<>();
        fragments.add(DailyDataFragment.newInstance());
        fragments.add(ZhenliaoListFragment.newInstance());
        fragments.add(TiaoliListFragment.newInstance());
        adapter = new FragmentTabAdapter(getSupportFragmentManager(), fragments);
        viewpager.setAdapter(adapter);


    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        setCurrent(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.data_daily:
                setCurrent(0);
                break;
            case R.id.data_zhenliao:
                setCurrent(1);
                break;
            case R.id.data_tiaoli:
                setCurrent(2);
                break;
        }
    }

    @Override
    public void onTitleBackClicked() {
        finish();
    }

    @Override
    public void onTitleRightClicked() {

    }

    @Override
    public void onTitleClicked() {

    }

    private void setCurrent(int currentIndex) {

        RadioButton radioButton = (RadioButton) radioGroup.getChildAt(currentIndex);
        if (!radioButton.isChecked()) {
            radioButton.setChecked(true);
        }

        if (viewpager.getCurrentItem() != currentIndex) {
            viewpager.setCurrentItem(currentIndex);
        }

    }
}
